<?php
include_once "baseChannel.php";

class weiwang extends baseChannel {
    public $appid;
    public $apikey;
    public $url;

    public function __construct($setting){
        parent::__construct($setting);
        $this->appid = $setting['appid'];
        $this->apikey = $setting['apikey'];
        $this->url = $setting['url'];
    }
    public function getSign($params){
        unset($params['sign']);
        ksort($params);
        $params['key'] = $this->apikey;
        $str = http_build_query($params);
//        echo "---类加密*$str*\n";

        return strtoupper(md5($str));
    }
    //下单
    public function submitOrder($mobile, $code, $params=[]){
        $url = $this->url.'/api/flow/recharge';
        $post_data = [
            'appid'=>$this->appid,
            'timestamp'=>$this->timestamp,
            'mobile'=>$mobile,
            'code'=>$code,
            'order_sn'=>$params['push_order_sn']
        ];
        $post_data['sign'] = $this->getSign($post_data);
        $return_str = $this->httpRequest($url,'POST',$post_data);
        $return_data = json_decode($return_str,1);
        //failure
        if($return_data['error']==0){
            return [
                'error'=>0,
                'msg'=>$return_data['msg'],
                'channel_order'=>$return_data['p_order_sn'],
                'post_data'=>json_encode($post_data),
                'channel_data'=>$return_str,
                'code' => 200
            ];
        }else{
            //余额不足
            if($return_data['code']==101){
                return [
                    'error'=>1,
                    'msg'=>$return_data['msg'],
                    'remark'=>$return_data['msg'],
                    'post_data'=>json_encode($post_data),
                    'channel_data'=>$return_str,
                    'code'=>2000
                ];
            }
            return [
                'error'=>1,
                'msg'=>$return_data['msg'],
                'remark'=>$return_data['msg'],
                'post_data'=>json_encode($post_data),
                'channel_data'=>$return_str,
                'code'=>500
            ];
        }
    }

    public function callback($params){

        $this->callback_params = $params;

        $this->channel_order_sn = trim($params['p_order_sn']);
        $this->push_order_sn = $params['order_sn'];

        //返回参数
        $res = $this->initCallBackParam();

        $order_status = $params['order_status'];
        $msg = $params['fail_msg'];


        $sign = $this->getSign($params);
        /*
        if($sign != $params['sign']){
            $res['error'] = 1;
            $res['msg'] = '签名不匹配';
            $res['resp_data'] = 'fail';
            return $res;
        }
        */

        if($order_status==3){
            $res['order_status'] = self::OrderSuccess;
            $res['msg'] = $msg ?? '充值完成';
        }

        if($order_status==4){
            $res['order_status'] = self::OrderFail;
            $res['msg'] = $msg ?? '充值失败';
        }
        return $res;

    }
}
